package com.itfreer.gis.server.utils.minIO;

import java.io.File;

import io.minio.MinioClient;
import io.minio.policy.PolicyType;

/**
 * 定义GeoServer TMS切片数据上传代码
 */
public class minIO {

	public static void uploader(String basePath,String bucket,String name) {
		try {
			File baseFile = new File(basePath);
			// 1、层级
			File[] cjList = baseFile.listFiles();
			if (cjList == null) {
				return;
			}

			// 使用MinIO服务的URL，端口，Access key和Secret key创建一个MinioClient对象
			MinioClient minioClient = new MinioClient("http://47.106.79.19:9000", "minio", "123!@#qweQWE@ITFREER");

			// 检查存储桶是否已经存在
			boolean isExist = minioClient.bucketExists(bucket);
			if (!isExist) {
				// 创建一个名为asiatrip的存储桶，用于存储照片的zip文件。
				minioClient.makeBucket(bucket);
				minioClient.setBucketPolicy(bucket, "*", PolicyType.READ_WRITE);
			}

			for (File cj : cjList) {	 //层级文件夹
				String cjName = cj.getName();
				// 行目录
				File[] knList = cj.listFiles();
				if (knList != null) {
					for (File kn : knList) {
						// 3、列目录
						File[] pngList = kn.listFiles();
						if (pngList != null) {
							for (File pngFile : pngList) {
								String key = name + "/" + cjName + "/" + kn.getName() + "/" + pngFile.getName();
								minioClient.putObject(bucket, key, pngFile.getPath(), "application/x-protobuf");
							}
						}
					}
				}
			}
		} catch (Exception e) {
			System.out.println("Error occurred: " + e);
		}
	}

}
